package com.example.home.controller;

import com.example.home.Dao.CourseRepository;
import com.example.home.Entities.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("courses")
public class CourseController {

    @Autowired
    CourseRepository courseRepository;

    @GetMapping
    public String outStudentForm1(Model model){
        List<Course> course1 = courseRepository.findAll();
        model.addAttribute("course2",course1);
        return "course/list-course";
    }


    @GetMapping("/new")
    public String outStudentForm(Model model){

        Course courses = new Course();
        model.addAttribute("course",courses);
        return "course/Course";
    }

    @PostMapping("/save")
    public String CreateStudent(Model model,Course course){

        //处理保存数据库的行为
       courseRepository.save(course);
        //使用定位防止重复提交
        return "redirect:/courses/new";

    }

}
